//
//  fundSumViewController.swift
//  NewLndd
//
//  Created by 邵禹曦 on 2020/1/7.
//  Copyright © 2020 辽宁当代. All rights reserved.
//

import UIKit
// MARK: - 资金
class fundSumViewController: HomeApiViewController {
    
    //挂单冻结资金：OFF
    @IBOutlet weak var OFF: UILabel!
    //当日订购货款：PP
    @IBOutlet weak var PP: UILabel!
    
    @IBOutlet weak var AP: UILabel!
    
    @IBOutlet weak var TF: UILabel!
    
    @IBOutlet weak var PF: UILabel!
    
    @IBOutlet weak var AF: UILabel!
    
    @IBOutlet weak var TPMB: UILabel!
    
    @IBOutlet weak var DGDJ: UILabel!
    
    @IBOutlet weak var BHDJ: UILabel!
    
    @IBOutlet weak var DRMC: UILabel!
    
    @IBOutlet weak var PFF: UILabel!
    
    @IBOutlet weak var AFF: UILabel!
    
    @IBOutlet weak var AT: UILabel!
    
    @IBOutlet weak var DF: UILabel!
    
    @IBOutlet weak var dff: UILabel!
    
    @IBOutlet weak var hf: UILabel!
    
    @IBOutlet weak var dhff: UILabel!

    override func viewDidLoad() {
        super.viewDidLoad()
        showInfo() 
    }
    


}
extension fundSumViewController {
    // 用户验证
    private func showInfo() {
           // GCD
           let group = DispatchGroup()
    
           // 从沙盒中获取参数
            var userPath = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true).first!
            userPath =  (userPath as NSString).appendingPathComponent("data.plist")
            user = NSKeyedUnarchiver.unarchiveObject(withFile: userPath) as? User
        
        Thread.sleep(forTimeInterval: 0.05)


           // 用户验证
           self.u = user?.U!
           self.retcode = user?.RETCODE!
           // 用户验证
           let str = [u,"101",retcode]
            
           //self.isLogin = self._network(str: str as! [String], group: group)
        group.enter()
        NetworkTools.share.check_user(code: str as Array<Any>) { (result, error) in
                      
                          if error != nil {
                              print("no no no")
                              return
                          }
                          

                          if Int(self._retcode(res: result!))! < 0 {
                              self.isLogin = false
                          } else {
                              self.isLogin = true
                          }
                          group.leave()
                      }

           group.notify(queue: DispatchQueue.main) {
               if !self.isLogin {
                   return self.setupVisitorView()
               } else {
                
                Thread.sleep(forTimeInterval: 0.05)
                    // 2.设置导航栏内容
                    self.tableShouInfo()
                
               }
            }
       }
}


extension fundSumViewController {
    //MARK: - 资金部分展示
    @objc private func tableShouInfo() {
        
        let str = [user?.U!, user?.RETCODE!]
        
        NetworkTools.share.firm_info(code: str as Array<Any>) { (result, error) in
            if error != nil {
                print("no no no")
                return
            }
            

            
            // 获取可选类型中的数组
            guard let resultArray = result else {
                print("no")
                return
            }
            
            
            
            if let REP = resultArray["REP"] as? [String : AnyObject] {
                if let resultList = REP["RESULT"] as? [String : AnyObject] {
                    
                    self.OFF.text = (resultList["OFF"] as? String)
                    self.PP.text = (resultList["PP"] as? String)
                    self.AP.text = (resultList["AP"] as? String)
                    self.TF.text = (resultList["TF"] as? String)
                    self.PF.text = (resultList["PF"] as? String)
                    self.AF.text = (resultList["AF"] as? String)
                    self.TPMB.text = (resultList["TPMB"] as? String)
                    self.DGDJ.text = (resultList["DFPM"] as? String)
                    self.AFF.text = (resultList["APPM"] as? String)
                    self.DRMC.text = (resultList["TPMS"] as? String)
                    self.PFF.text = (resultList["PFF"] as? String)
                    self.AFF.text = (resultList["AFF"] as? String)
                    self.AT.text = (resultList["AT"] as? String)
                    self.DF.text = (resultList["DF"] as? String)
                    self.dff.text = (resultList["DFF"] as? String)
                    self.hf.text = (resultList["HF"] as? String)
                    self.dhff.text = (resultList["DHFF"] as? String)
                }
            }
        }
    }
}
